<template>
  <div class="participant wrap-table userInfo-list">
    <el-form
      :model="addForm1"
      :rules="rules1"
      ref="addForm1"
      label-width="140px"
      class="demo-ruleForm"
      v-loading="loading"
    >
      <el-form-item label="姓名首字母" prop="Surname">
        <el-input v-model="addForm1.Surname"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="Mobile">
        <el-input v-model="addForm1.Mobile" :disabled="addForm1.UserCode!==''"></el-input>
      </el-form-item>
      <el-form-item label="研究编号" prop="ResearchNo">
        <el-input v-model="addForm1.ResearchNo" :disabled="RoleGroup>1"></el-input>
      </el-form-item>
      <el-form-item label="登记日期">{{addForm1.CreateTime}}</el-form-item>
      <el-form-item label="入组时研究阶段">
        <el-select v-model="addForm1.InPeriod" placeholder="请选择" :disabled="InDisabled">
          <el-option
            v-for="item in Inoptions1"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <span class="span_right">入组日期{{addForm1.ConfirrmTime | ConfirrmTime}}，入组三天内可以修改</span>
      </el-form-item>
      <el-form-item label="当前研究阶段" prop="Period">
        <el-select v-model="addForm1.Period" placeholder="请选择" @change="changeIsCHIld">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--儿童期-->
      <div v-if="addForm1.Period==3 || addForm1.Period==4">

      <el-form-item label="分娩日期" prop="ChildBirthDate" >
        <el-date-picker
          v-model="addForm1.ChildBirthDate"
          type="date"
          :picker-options="pickerOptions1"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-row >
        <el-col :span="12">
          <el-form-item label="出生孕周" prop="BirthWeek">
            <el-input v-model="addForm1.BirthWeek" type="number" :min="0" placeholder="两位数整数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="天" prop="BirthDay">
            <el-input v-model="addForm1.BirthDay" type="number"  placeholder="0-6"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="分娩方式" prop="DelivryMode">
        <el-select v-model="addForm1.DelivryMode" placeholder="请选择">
          <el-option label="阴道分娩" value="1"></el-option>
          <el-option label="剖宫产" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="儿童性别" prop="ChildSex" >
        <el-select v-model="addForm1.ChildSex" placeholder="请选择">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生体重" prop="BirthWeight" >
        <el-input v-model="addForm1.BirthWeight" type="number" @blur="getWeight">
          <template slot="append">g</template>
        </el-input>
          <span class="span_right" style="color:#f00;right:-420px">{{weightSpan}}</span>
      </el-form-item>
      </div>
      <!--儿童期-->
      <el-form-item label="末次月经首日日期" prop="LastMenseDate" v-if="addForm1.Period==2">
        <el-date-picker
          v-model="addForm1.LastMenseDate"
          type="date"
          @change="changeDate1"
          :picker-options="pickerOptions1"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="预产期" prop="ExpectedDate" v-if="addForm1.Period==2" class="bulrinp">
        <el-date-picker
          v-model="addForm1.ExpectedDate"
          type="date"
          @change="changeIsP"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="纠正预产期" v-if="addForm1.Period==2" class="bulrinp">
        <el-date-picker
          v-model="addForm1.RecorrectExpecteDate"
          type="date"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="入组时所在地区" prop="InDistrictName">
        <el-select
          v-model="addForm1.InDistrictName"
          placeholder="请选择"
          @change="InchangeDisTwo"
          :disabled="RoleGroup>1"
        >
          <el-option
            v-for="item in InoptionDiq"
            :key="item.DistrictName+Math.random()"
            :label="item.DistrictName"
            :value="item.DistrictName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入组时所在社区" prop="InCommunityId">
        <el-select
          v-model="InCommunityId"
          placeholder="请先选择所在地区"
          @change="InCommuty"
          :disabled="RoleGroup>1"
        >
          <el-option
            v-for="item in typeInto"
            :key="item.SYS_ID+Math.random()"
            :label="item.CName"
            :value="item.CName"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="研究组别" prop="GroupType">
        <el-select
          v-model="addForm1.GroupType"
          placeholder="请选择"
          @change="changeIs"
          :disabled="RoleGroup>1"
        >
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="当前所在地区" prop="DistrictName">
        <el-select v-model="addForm1.DistrictName" placeholder="请选择" @change="changeDisTwo">
          <el-option
            v-for="item in optionDiq"
            :key="item.DistrictName+Math.random()"
            :label="item.DistrictName"
            :value="item.DistrictName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="当前所在社区" prop="CommunityId">
        <el-select v-model="CommunityId" placeholder="请先选择所在地区" @change="Commuty">
          <el-option
            v-for="item in options4"
            :key="item.SYS_ID+Math.random()"
            :label="item.CName"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="转移社区原因">
        <el-input v-model="addForm1.TransforReason"></el-input>
      </el-form-item>
      <el-form-item label="产前干预强度" v-if="addForm1.GroupType=='1'">
        {{addForm1.IntensityOfIntervention |IntensityOfIntervention}}
        <span style="font-size:12px;color:#7F7F7F;padding-left:10px;">(孕前期、怀孕期干预强度)</span>
        <span
          class="span_right"
          style="right: -250px"
          v-if="addForm1.InIntensityOfIntervention !=1 &&  addForm1.TransforIntensityTime"
        >
          (A->AA
          时间{{addForm1.TransforIntensityTime}})
        </span>
      </el-form-item>
      <el-form-item label="儿童(CH1)干预强度">
        {{addForm1.ChildIntensityOfIntervention |IntensityOfIntervention}}
        <span
          class="span_right"
          style="right: -250px"
          v-if="addForm1.ChildTransforIntensityTime"
        >
          (A->AA时间 {{addForm1
          .ChildTransforIntensityTime}})
        </span>
      </el-form-item>
      <el-form-item label="儿童(CH2)干预强度">
        {{addForm1.Child2IntensityOfIntervention |IntensityOfIntervention}}
        <span
          class="span_right"
          style="right: -250px"
          v-if="addForm1.Child2TransforIntensityTime"
        >
          (A->AA时间 {{addForm1
          .Child2TransforIntensityTime}})
        </span>
      </el-form-item>
      <el-form-item label="CH1转CH2日期" >
        <el-date-picker
          v-model="addForm1.CH1ToCH2Time"
          type="date"
          disabled
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
        <span class="span_right" style="right: -245px" v-if="addForm1.ToInactiveDate">
          {{(addForm1
          .CH1ToCH2Age)}}
        </span>
      </el-form-item>
      <el-form-item label="发放身高体重测量仪" prop="UserState">
        <span>{{addForm1.IsGevenEquipment == '1' ? '是'  : addForm1.IsGevenEquipment == '2' ?'否' :'-'}}</span>
      </el-form-item>
      <el-form-item label="用户状态" prop="UserState">
        <el-select v-model="addForm1.UserState" placeholder="请选择">
          <el-option
            v-for="item in options3"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <span class="span_right" style="right: -245px" v-if="addForm1.ToInactiveDate">
          转为不活跃状态的时间{{addForm1
          .ToInactiveDate}}
        </span>
      </el-form-item>
      <el-form-item label="用户状态说明">
        <el-input v-model="addForm1.UserStateExplain"></el-input>
      </el-form-item>

      <el-form-item style="width: 500px;" class="clearfix">
        <el-button type="primary" class="pd-bt pull-left" @click="nextStep1('addForm1')">保存</el-button>
        <el-button
          v-if="oldPeriod=='2'&& addForm1.IsChildBorn !='1'"
          class="pd-bt pull-left"
          style="margin-left: 50px;"
          @click="bornSure()"
        >宝宝出生了</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <span>您修改了研究对象的“预产期”信息，是否为研究对象重新生成日程待办事项？重置日程后已预约、已完成事项会被保留，此外的日程事项将会按日程模板重新生成，请谨慎操作！</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noReset(addForm1)">修改预产期但不重置日程</el-button>
        <el-button type="primary" @click="Reset(addForm1)">修改预产期并重置日程</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import {DIC_PERIODB,filterIntensityOfIntervention} from '@/utils/localDic'
var Vm = null;
export default {
  data() {
    var validateYanj1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("必填项"));
      } else if (!/^[a-zA-Z0-9-]+$/.test(value)) {
        callback(new Error("允许英文字母及数字"));
      } else {
        callback();
      }
    };
    var isSUrename = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入姓名"));
      } else if (!/^[a-zA-Z]{1,4}$/.test(value)) {
        callback(new Error("请输入字母，最多4位"));
      } else {
        callback();
      }
    };
    var isvalidUserphone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号"));
      } else if (!/^1[2|3|4|5|6|7|8|9][0-9]{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    return {
      addForm1: {
        Surname: "",
        Name: "",
        Mobile: "",
        ResearchNo: "",
        Period: "",
        LastMenseDate: "",
        ExpectedDate: "",
        CommunityId: "",
        GroupType: "",
        CreateTime: "",
        IntensityOfIntervention: "2",
        UserStateExplain: "",
        ConditionFillState: "4",
        InPeriod: "",
        DistrictNo: "",
        DistrictName: "",
        ChildBirthDate: "",
        BirthWeek: "",
        BirthDay: "",
        DelivryMode: "",
        ChildSex: "",
        BirthWeight: "",
        InCommunityId: "",
        InDistrictNo: "",
        InDistrictName: "",
      },
      weightSpan:"",
      ToInactiveDate: "",
      loading: true,
      CommunityId: "",
      InCommunityId: "",
      dialogVisible: false,
      ExpectedDate: "", // 记录上次孕产期
      isLoand: false,
      rules1: {
        ChildBirthDate: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        BirthWeek: [{ required: true, message: "必填项", trigger: "blur" }],
        BirthDay: [{ required: true, message: "必填项", trigger: "blur" }],
        DelivryMode: [{ required: true, message: "必填项", trigger: "blur" }],
        ChildSex: [{ required: true, message: "必填项", trigger: "blur" }],
        BirthWeight: [{ required: true, message: "必填项", trigger: "blur" }],
        Surname: [{ validator: isSUrename, trigger: "blur", required: true }],
        Name: [{ required: true, message: "必填项", trigger: "blur" }],
        Mobile: [
          { validator: isvalidUserphone, trigger: "blur", required: true },
        ],
        ResearchNo: [
          { validator: validateYanj1, required: true, trigger: "blur" },
        ],
        Period: [{ required: true, message: "必填项", trigger: "blur" }],
        LastMenseDate: [{ required: true, message: "必填项", trigger: "blur" }],
        ExpectedDate: [{ required: true, message: "必填项", trigger: "blur" }],
        CommunityId: [{ required: true, message: "必填项", trigger: "blur" }],
        DistrictName: [{ required: true, message: "必填项", trigger: "blur" }],
        InCommunityId: [{ required: true, message: "必填项", trigger: "blur" }],
        InDistrictName: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
      },
      itemType: "",
      sysId: "",
      mobile: "",
      pickerOptions1: {
        disabledDate: function (val) {
          if (val > new Date()) return true;
        },
      },
      pickerOptions: {
        disabledDate: function (val) {
          if (val < new Date(Vm.addForm1.LastMenseDate)) return true;
        },
      },
      researchNo: "",
      options1:DIC_PERIODB,
      Inoptions1: [
        { value: "1", label: "孕前期" },
        { value: "2", label: "怀孕期" },
      ],
      options2: [
        { value: "1", label: "A" },
        { value: "2", label: "B" },
      ],
      options4: "",
      options3: [
        { value: "1", label: "活跃" },
        { value: "2", label: "不活跃" },
      ],
      InDisabled: false,
      optionDiq: [],
      typeInto: [],
      InoptionDiq: [],
      oldPeriod: "",
      oldUserState: "",
    };
  },
  props: {
    changeIndex: {
      type: String,
    },
  },
  watch: {
    InCommunityId(val) {
      console.log(val);
    },
    changeIndex: function (index) {
      if (index == "0") {
        this.getList();
      }
    },
    "addForm1.GroupType": function (val) {
      if (
        val == 1 &&
        this.isLoand &&
        this.addForm1.IntensityOfIntervention != 1
      ) {
        this.addForm1.IntensityOfIntervention = 2;
      }
    },
    "addForm1.Surname": function (val) {
      this.addForm1.Surname = val.toUpperCase();
    },
    "addForm1.ConfirrmTime": function (val) {
      //计算当前时间与入组时间比较,3天以内包含三天可以修改
      if (val == undefined || val == "") {
        this.InDisabled = true;
      } else {
        let time1 = new Date().getTime();
        let time2 = new Date(val.substring(0, 10)).getTime();
        let days = parseInt(Math.abs(time1 - time2) / 1000 / 24 / 60 / 60);
        days > 3 ? (this.InDisabled = true) : (this.InDisabled = false);
      }
    },
  },
  computed: {
    ...mapGetters(["resechObj", "RoleGroup", "userIcon", "token"]),
  },
  components: {},
  filters: {
    IntensityOfIntervention:filterIntensityOfIntervention,
    ConfirrmTime: function (val) {
      if (val == undefined) {
        return "-";
      } else {
        return val.substring(0, 10);
      }
    },
  },
  created() {},
  methods: {
    getWeight(e) {
      this.service({
        url: "ResearchObject/GetBirthWeightPercentile",
        method: "get",
        params: {
          sex: this.addForm1.ChildSex ,
          birthAge: this.addForm1.BirthWeek,
        },
      }).then((res) => {
        if(res.dtData.length>0){
          if( parseInt(res.dtData[0].P10)> parseInt(e.target.value)){
            this.weightSpan = "出生体重<P10，请在健康现况中勾选“此次分娩诊断小于胎龄儿"
          }
          else if(parseInt(res.dtData[0].P90) < parseInt(e.target.value)){
            this.weightSpan = "出生体重>P90，请在健康现况中勾选“此次分娩诊断大于胎龄儿 "
          }
          else{
            this.weightSpan =''
          }
        }
        console.log(res);
      });
    },
    bornSure() {
      //宝宝出生了
      let that = this;
      that
        .$confirm("请确认宝宝是否已经出生了？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          that
            .service({
              url: "ResearchObject/ChildBorn",
              method: "post",
              data: {
                OpId: that.token,
                OpName: that.userIcon,
                ResearchObjectId: that.sysId,
              },
            })
            .then((resp) => {
              that.$router.push({
                path: "/participant/changePeriod",
                query: {
                  isChild: true,
                },
              });
            })
            .catch((data) => {
              console.log(data);
            });
        })
        .catch(() => {
          that.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    getList: function () {
      //获取一级信息列表
      let that = this;
      that.addForm1 = {
        Surname: "",
        Name: "",
        Mobile: "",
        ResearchNo: "",
        Period: "",
        LastMenseDate: "",
        ExpectedDate: "",
        CommunityId: "",
        GroupType: "",
        CreateTime: "",
        IntensityOfIntervention: "2",
        UserStateExplain: "",
        ConditionFillState: "4",
        InPeriod: "",
        DistrictNo: "",
        BirthWeek: "",
        BirthDay: "",
        DistrictName: "",
        InCommunityId: "",
        InDistrictNo: "",
        InDistrictName: "",
      };
      if (that.resechObj.mobile || that.resechObj.researchNo) {
        that.sysId = "";
        that.mobile = that.resechObj.mobile;
        that.researchNo = that.resechObj.researchNo;
      }
      that.$nextTick(function () {
        that.$refs["addForm1"].resetFields();
      });
      that
        .service({
          url: "/ResearchObject/GetParticipantDetail",
          method: "get",
          params: {
            itemType: "1",
            period: "",
            sysId: that.sysId,
            mobile: that.mobile,
            researchNo: that.researchNo,
            pageCount: 0,
            pageSize: 100,
            researchObjectId: "",
          },
        })
        .then((response) => {
          console.log(response);
          that.ExpectedDate = response.dtData[0].ExpectedDate
            ? response.dtData[0].ExpectedDate
            : "";
          response.dtData[0] ? (that.addForm1 = response.dtData[0]) : "";
         
          that.oldPeriod = response.dtData[0].Period;
          if (response.dtData.length == 0) {
            that.$notify.info({
              title: "提示",
              message: "未查找到相关数据",
            });
          }
          that.CommunityId = response.dtData[0].CName || "'";
          that.InCommunityId = response.dtData[0].InCName || "";
          that.oldUserState = response.dtData[0].userState;
          that.loading = false;
          that.getComselectList();
          that.IngetComselectList();
          console.log(this.addForm1);
        })
        .catch((data) => {
          that.loading = false;
        });
    },
    GetCommunityDistrict: function () {
      //获取社区行政区
      let that = this;
      that
        .service({
          url: "/Dic/GetCommunityDistrict",
          method: "get",
          params: {},
        })
        .then((response) => {
          that.optionDiq = response.dtData;
          that.InoptionDiq = response.dtData;
        })
        .catch((data) => {});
    },
    InchangeDisTwo: function (val) {
      this.InCommunityId = "";
      if (val != "") {
        this.addForm1.InDistrictNo =
          this.InoptionDiq.find((vals) => {
            if (vals.DistrictName == val) {
              return vals;
            }
          }).DistrictNo || "";
        this.addForm1.InCommunityId = "";
        this.IngetComselectList();
      } else {
        this.addForm1.InCommunityId = "";
        this.typeInto = [];
      }
    },
    InCommuty: function (val) {
      this.addForm1.InCommunityId = this.typeInto.filter((item) => {
        return item.CName == val;
      })[0].SYS_ID;
    },
    changeDisTwo: function (val) {
      this.CommunityId = "";
      if (val != "") {
        this.addForm1.DistrictNo =
          this.optionDiq.find((vals) => {
            if (vals.DistrictName == val) {
              return vals;
            }
          }).DistrictNo || "";
        this.addForm1.CommunityId = "";
        this.getComselectList();
      } else {
        this.addForm1.CommunityId = "";
        this.options4 = [];
      }
    },
    Commuty: function (val) {
      console.log(this.addForm1);
      if (this.addForm1.GroupType != val.CGroup) {
        this.addForm1.CommunityId = "";
        this.CommunityId = "";
        this.$message({
          title: "提示",
          type: "info",
          message: "仅支持转入相同研究组别的社区（A->A，B->B）",
        });
        return false;
      }
      this.CommunityId = val.CName;
      this.addForm1.CommunityId = val.SYS_ID;
      this.addForm1.GroupType = val.CGroup;
    },
    changeDate1: function (val) {
      //末次月经时间改变
      let that = this;
      let newdata = new Date(val); //计算预产期
      that.isLoand = true;
      if (val !== "") {
        console.log(getNewData(dateForm(newdata),280))
        that.$set(
          that.addForm1,
          "ExpectedDate",
          getNewData(dateForm(newdata),280)
        );
      }
    },
    getComselectList: function () {
      //获取社区信息
      let that = this;
      that
        .service({
          url: "dic/GetCommunity",
          method: "get",
          params: {
            cName: "",
            cGroup: "",
            pageCount: 0,
            districtNo: that.addForm1.DistrictNo || "",
            pageSize: 100,
          },
        })
        .then((resp) => {
          that.options4 = resp.dtData;
        })
        .catch((data) => {});
    },
    IngetComselectList: function () {
      //获取社区信息
      let that = this;
      that
        .service({
          url: "dic/GetCommunity",
          method: "get",
          params: {
            cName: "",
            cGroup: "",
            pageCount: 0,
            districtNo: that.addForm1.InDistrictNo || "",
            pageSize: 100,
          },
        })
        .then((resp) => {
          that.typeInto = resp.dtData;
        })
        .catch((data) => {});
    },
    changeIsCHIld: function (val) {
      //IsUltrasonicChecked 判断胎心B超以及宝宝是否出生1为已确认，2为未确认
      let that = this;
      if (that.oldPeriod == 1 && val == 2) {
        if (that.addForm1.IsUltrasonicChecked != 1) {
          that.$alert(
            "请在转组管理中修改其胎心B超确认情况为“已确认”。",
            "提示",
            {
              confirmButtonText: "知道了",
              callback: (action) => {
                that.$set(that.addForm1, "Period", "1");
              },
            }
          );
          return false;
        }
        that.isLoand = true;
      } else if (that.oldPeriod == 1 && val == 3) {
        //当前为孕前期，不允许直接跳到儿童期
        that.$alert("研究对象不可以从孕前期转组到儿童期。", "提示", {
          confirmButtonText: "知道了",
          callback: (action) => {
         that.$set(that.addForm1, "Period", "1");
          },
        });
        that.isLoand = true;
        return false;
      } else if (that.oldPeriod == 2 && val == 3) {
        if (that.addForm1.IsChildBorn != 1) {
          that.$alert(
            "请先点击“宝宝出生了”，为其创建儿童期转组档案。并在转组档案中确认宝宝出生。",
            "提示",
            {
              confirmButtonText: "知道了",
              callback: (action) => {
               that.$set(that.addForm1, "Period", "2");
              },
            }
          );
          return false;
        }
        that.isLoand = true;
      } else {
        this.isLoand = true;
      }
    },
    changeIs: function (val) {
      this.isLoand = true;
    },
    changeIsP: function (val) {
      this.isLoand = true;
    },
    noReset(formName) {
      /// 修改但不重置日程
      let that = this;
      this.dialogVisible = false;
      if (that.isLoand) {
        that.loading = true;
        that
          .service({
            url: "/ResearchObject/SaveResearchObject",
            method: "post",
            data: {
              ...that.addForm1,
              IsUpdate: "1",
              OpId: that.token,
              OpName: that.userIcon,
            },
          })
          .then((response) => {
            that.loading = false;
            that.$message({
              type: "success",
              message: response.msg,
            });
          })
          .catch((data) => {
            that.loading = false;
          });
      } else {
        that
          .service({
            url: "/ResearchObject/SaveResearchObject",
            method: "post",
            data: {
              ...that.addForm1,
              IsUpdate: "1",
            },
          })
          .then((response) => {
            that.$message({
              type: "success",
              message: response.msg,
            });
            that.$router.push({
              path: "/participant/editor",
              query: {
                id: that.sysId,
                mobile: that.mobile,
                researchNo: that.addForm1.ResearchNo,
              },
            });
            that.loading = false;
          })
          .catch((data) => {
            that.loading = false;
          });
      }
    },
    Reset(data) {
      // 修改并重置日程
      this.dialogVisible = false;
      this.save(data);
    },
    nextStep1: function (formName) {
      //下一步  添加研究对象
      let that = this;
      if (that.addForm1.Period == 1) {
        that.addForm1.LastMenseDate = "";
        that.addForm1.ExpectedDate = "";
      }
      if (that.addForm1.GroupType == 2) {
        that.addForm1.IntensityOfIntervention = "";
      }
      that.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            that.oldPeriod == 2 &&
            that.addForm1.ExpectedDate != that.ExpectedDate
          ) {
            this.dialogVisible = true;
          } else {
            this.save(formName);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    save(formName) {
      var that = this;
      if (
        !this.addForm1.ToInactiveDate &&
        this.addForm1.UserState != this.oldUserState &&
        this.addForm1.UserState == "2"
      ) {
        this.addForm1.ToInactiveDate = dateForm(new Date());
      }
      if (that.isLoand) {
        that.loading = true;
        that
          .service({
            url: "/ResearchObject/SaveResearchObject",
            method: "post",
            data: {
              ...that.addForm1,
              IsUpdate: "1",
              OpId: that.token,
              OpName: that.userIcon,
            },
          })
          .then((response) => {
            that.$message({
              type: "success",
              message: response.msg,
            });
            that
              .service({
                url: "/ResearchObject/CreateSchedule",
                method: "post",
                data: {
                  SYS_ID: that.sysId,
                },
              })
              .then((response) => {
                that.loading = false;
                that.isLoand = false;
                that
                  .$alert("已更新研究对象日程！", "提示", {
                    confirmButtonText: "知道了",
                    showClose: false,
                  })
                  .then(() => {
                    that.$router.push({
                      path: "/participant/editor",
                      query: {
                        id: that.sysId,
                        mobile: that.mobile,
                        researchNo: that.addForm1.ResearchNo,
                      },
                    });
                  });
              })
              .catch((data) => {
                that.loading = false;
              });
          })
          .catch((data) => {
            that.loading = false;
          });
      } else {
        that
          .service({
            url: "/ResearchObject/SaveResearchObject",
            method: "post",
            data: {
              ...that.addForm1,
              OpId: that.token,
              OpName: that.userIcon,
              IsUpdate: "1",
            },
          })
          .then((response) => {
            that.$message({
              type: "success",
              message: response.msg,
            });
            that.$router.push({
              path: "/participant/editor",
              query: {
                id: that.sysId,
                mobile: that.mobile,
                researchNo: that.addForm1.ResearchNo,
              },
            });
            that.loading = false;
          })
          .catch((data) => {
            that.loading = false;
          });
      }
    },
  },
  mounted: function () {
    let that = this;
    that.sysId = that.$route.query.id;
    that.getList();
    that.GetCommunityDistrict();
    Vm = this;
  },
};
function getNewData(dateTemp, days) {  
   var dateTemp = dateTemp.split("-");  
   var nDate = new Date(dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]); //转换为MM-DD-YYYY格式    
   var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);  
   var rDate = new Date(millSeconds);  
   var year = rDate.getFullYear();  
   var month = rDate.getMonth() + 1;  
   if (month < 10) month = "0" + month;  
   var date = rDate.getDate();  
   if (date < 10) date = "0" + date;  
   return (year + "-" + month + "-" + date);  
} 
function addZero(s) {
  return s < 10 ? "0" + s : s;
}
function dateForm(data) {
  return (
    new Date(data).getFullYear() +
    "-" +
    addZero(new Date(data).getMonth() + 1) +
    "-" +
    addZero(new Date(data).getDate())
  );
}
function WeeksBetw(date1, date2) {
  //计算日期之间相差得天数
  var _dt1 = new Date(date1);
  var _dt2 = new Date(date2);
  var dt1 = _dt1.getTime();
  var dt2 = _dt2.getTime();
  return parseInt(Math.abs(dt1 - dt2) / 1000 / 60 / 60 / 24); //这里是计算天数,如果想获得周数则在该返回值上再除以7
}
</script>
<style scoped lang="scss">
.userInfo-list {
  max-width: 40%;
  .span_right {
    position: absolute;
    top: 0;
    right: -300px;
    color: #666666;
  }
}
</style>
